package com.zbh.uexam.controller.student;

import com.zbh.uexam.base.BaseApiController;
import com.zbh.uexam.base.RestResponse;
import com.zbh.uexam.domain.ExamPaper;
import com.zbh.uexam.domain.ExamPaperAnswer;
import com.zbh.uexam.domain.enums.ExamPaperTypeEnum;
import com.zbh.uexam.repository.ExamPaperAnswerMapper;
import com.zbh.uexam.service.ExamPaperAnswerService;
import com.zbh.uexam.service.ExamPaperService;
import com.zbh.uexam.utility.DateTimeUtil;
import com.zbh.uexam.utility.ExamUtil;
import com.zbh.uexam.utility.JsonUtil;
import com.zbh.uexam.utility.PageInfoHelper;
import com.zbh.uexam.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.zbh.uexam.viewmodel.student.exam.ExamPaperPageResponseVM;
import com.zbh.uexam.viewmodel.student.exam.ExamPaperPageVM;
import com.github.pagehelper.PageInfo;
import com.zbh.uexam.viewmodel.student.exam.GenerateTrainingExamPaperVM;
import com.zbh.uexam.viewmodel.student.exam.SearchTrainingExamPaperParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController("StudentExamPaperController")
@RequestMapping(value = "/api/student/exam/paper")
public class ExamPaperController extends BaseApiController {

    Logger logger = LoggerFactory.getLogger(ExamPaperController.class);

    private final ExamPaperService examPaperService;
    private final ExamPaperAnswerService examPaperAnswerService;
    private final ApplicationEventPublisher eventPublisher;

    @Autowired
    public ExamPaperController(ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService, ApplicationEventPublisher eventPublisher) {
        this.examPaperService = examPaperService;
        this.examPaperAnswerService = examPaperAnswerService;
        this.eventPublisher = eventPublisher;
    }


    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> select(@PathVariable Integer id) {
        ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        if (vm.getPaperType().intValue() == ExamPaperTypeEnum.Task.getCode()) {
            ExamPaperAnswer examPaperAnswer = examPaperAnswerService.getByPidUid(id, getCurrentUser().getId());
            if (null != examPaperAnswer) {
                return RestResponse.fail(2, "任务试卷不能重复做");
            }
        }
        return RestResponse.ok(vm);
    }


    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperPageResponseVM>> pageList(@RequestBody @Valid ExamPaperPageVM model) {
        model.setLevelId(getCurrentUser().getUserLevel());
        PageInfo<ExamPaper> pageInfo = examPaperService.studentPage(model);
        logger.info("pageInfo:{}", JsonUtil.toJsonStr(pageInfo));
        PageInfo<ExamPaperPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            vm.setLimitStartTime(DateTimeUtil.dateFormat(e.getLimitStartTime()));
            vm.setLimitEndTime(DateTimeUtil.dateFormat(e.getLimitEndTime()));
            vm.setScore(ExamUtil.scoreToVM(e.getScore()));
            return vm;
        });
        logger.info("pageInfo:{}", JsonUtil.toJsonStr(page));
        return RestResponse.ok(page);
    }

    @RequestMapping(value = "/trainingPageList", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperPageResponseVM>> searchTrainingPage(@RequestBody SearchTrainingExamPaperParam searchTrainingExamPaperParam) {
        searchTrainingExamPaperParam.setUserId(getCurrentUser().getId());
        searchTrainingExamPaperParam.setLevelId(getCurrentUser().getUserLevel());
        PageInfo<ExamPaper> pageInfo = examPaperService.searchTrainingPage(searchTrainingExamPaperParam);
        PageInfo<ExamPaperPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            vm.setLimitStartTime(DateTimeUtil.dateFormat(e.getLimitStartTime()));
            vm.setLimitEndTime(DateTimeUtil.dateFormat(e.getLimitEndTime()));
            vm.setScore(ExamUtil.scoreToVM(e.getScore()));
            return vm;
        });
        logger.info("pageInfo:{}", JsonUtil.toJsonStr(page));
        return RestResponse.ok(page);
    }

    @RequestMapping(value = "/generateTrainingExamPaper", method = RequestMethod.POST)
    public RestResponse<String> generateTrainingExamPaper(@RequestBody GenerateTrainingExamPaperVM generateTrainingExamPaperVM) {
        return RestResponse.ok(examPaperService.generateTrainingExamPaper(generateTrainingExamPaperVM, getCurrentUser()));
    }

}
